<template>
  <div>
    <el-dialog
      title="修改密码"
      :visible.sync="show"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="form.newPassword" show-password></el-input>
        </el-form-item>
        <el-form-item label="再次确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" show-password></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["show"],
  data() {
    return {
      dialogVisible: true,
      form: {
        newPassword: "", //新密码
        confirmPassword: "", //确认密码
      },
      rules: {
        newPassword: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
        confirmPassword: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
      },
    };
  },
  watch: {},
  methods: {
    handleClose() {
      this.$emit("close", true);
    },
    confirm() {
      if (
        this.form.newPassword == this.form.confirmPassword &&
        this.$store.state.user.info.role != "2"
      ) {
        console.log(this.$store.state.user.info, "info");
        let obj = {
          username: this.$store.state.user.info.username,
          password: this.form.confirmPassword,
        };
        this.$http.post("/updatePassword", obj).then((res) => {
          if (res.code == 200) {
            this.$message.success("修改成功,请重新登陆");
            this.$parent.logout();
          } else {
            this.$message.error(res.msg);
          }
        });
      } else {
        this.$message.warning("1.请检查是否是超级管理员2.请检查密码是否一致");
      }
    },
  },
};
</script>

<style>
</style>